﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Core.Infrastructures.Cores.Injections
{
    public partial class Initialize
    {
        public static IContainer Container { get; private set; }

        public static Assembly[] ReferencedAssemblies
        {
            get => Assembly.GetEntryAssembly()
                .GetReferencedAssemblies()
                .Where(ass => ass.Name.StartsWith("Core"))
                .Select(Assembly.Load)
                .ToArray();
        }

        public static IServiceProvider Configure(IServiceCollection services)
        {
            var builder = new ContainerBuilder();
            builder.Populate(services);

            builder.RegisterAssemblyModules(typeof(Initialize).Assembly);  // 注册当前程序集所有模块

            Container = builder.Build();    // 创建容器池

            return new AutofacServiceProvider(Container);   // .NET Core 默认返回IServiceProvider接口实例
        }


    }
}
